#include "server-client-thread.h"
#include "../base/client-id.h"
#include <QtNetwork>
#include <iostream>
#include "../base/log.h"

ServerClientThread::ServerClientThread ( QObject *parent )
        : QThread (parent) {
    net_ = new ServerClientNet();
    //connect(net_, SIGNAL(message_received()), this, SLOT(recv_message()));
    bind_port_ = 0;
    std::cout << "am creat threadul de comunicare dintre client si server" << std::endl;
}

void ServerClientThread::run() {
    /*QTcpSocket tcpSocket;
    if ( !tcpSocket.setSocketDescriptor ( socketDescriptor ) )
    {
        emit error ( tcpSocket.error() );
        return;
    } */
/*
    QByteArray block;
    QDataStream out ( &block, QIODevice::WriteOnly );
    out.setVersion ( QDataStream::Qt_4_0 );
    out << ( quint16 ) 0;
    out << text;
    out.device()->seek ( 0 );
    out << ( quint16 ) ( block.size() - sizeof ( quint16 ) );

    tcpSocket.write ( block );
    tcpSocket.disconnectFromHost();
    tcpSocket.waitForDisconnected();*/
    LOG(INFO) << "am intrat in thread";
    Message *message;
    quint32 address;
    quint16 port;

    while (bind_port_) {
        LOG(INFO) << "am intrat in wile";
        if (net_->listening()) {
            net_->wait_for_ready_read();
            {
                // primim un mesaj
                net_->recv_message(&message, &address);
                // il stocam pentru ca alt thread sa il prelucreze
                enqueue_message(message, address);

                LOG(INFO) << "primit un mesaj de tip: " << message->type() << " port " << port;
                LOG(INFO) << "nr mesaje in coada " << messages_list_.size();
            }
        }
        //sleep(1);
    }

    LOG(INFO) << "am iesit din thread";
}


void ServerClientThread::create_recv_socket(quint16 port) {
    LOG(INFO) << "fac bind pe: " << port;
    net_->create_recv_socket(port);
    bind_port_ = port;
    start();
}

void ServerClientThread::send_message(quint32 address, quint16 port, Message* message) {
    // TODO: send_message nu trebuie apelat aici, ci in run
    net_->send_message(address, port, message);
}

void ServerClientThread::enqueue_message(const Message* message, quint32 address) {
    QMutexLocker locker(&mutex_);
    messages_list_.push_back(message);
    addresses_list_.push_back(address);
    emit message_received();
}

void ServerClientThread::head_message(const Message** message, quint32* address) {
    QMutexLocker locker(&mutex_);

    if (messages_list_.empty()) {
        *message = NULL;
        return;
    }

    *message = messages_list_.front();
    *address = addresses_list_.front();
}

void ServerClientThread::dequeue_message() {
    QMutexLocker locker(&mutex_);

    if (!messages_list_.empty()) {
        ASSERT(!addresses_list_.empty());

        //delete messages_list_.front();
        messages_list_.pop_front();
        addresses_list_.pop_front();
    }
}

quint32 ServerClientThread::queue_size() {
    QMutexLocker locker(&mutex_);
    return messages_list_.size();
}
